package com.chrray.position.service.position.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chrray.position.domain.dto.DictionaryDTO;
import com.chrray.position.exception.ServiceException;
import com.chrray.position.mapper.position.FdJobSubcategoryMapper;
import com.chrray.position.service.position.FdJobSubcategoryService;
import lombok.AllArgsConstructor;
import com.chrray.position.domain.FdJobSubcategory;
import com.chrray.position.domain.vo.FdJobSubcategoryVO;
import com.chrray.position.domain.dto.search.FdJobSubcategorySearch;
import com.chrray.position.domain.dto.FdJobSubcategoryDTO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 职位小类表 接口实现类
 *
 * @author wusiwee
 * @email wusiwee@163.com
 * @since 1.0.1 2024-08-07
 */
@Service
@AllArgsConstructor
public class FdJobSubcategoryServiceImpl extends ServiceImpl<FdJobSubcategoryMapper, FdJobSubcategory> implements FdJobSubcategoryService {
    private final FdJobSubcategoryMapper mapper;

    /**
     * 岗位选择列表
     */
    @Override
    public List<DictionaryDTO> selections() {
        List<DictionaryDTO> tree = new ArrayList<>();
        List<DictionaryDTO> all = mapper.findAll();
        List<DictionaryDTO> hots = all.stream().filter(item -> item.getHot() == 1).collect(Collectors.toList());
        tree.add(DictionaryDTO.asHot(hots));
        for (Map.Entry<Long, List<DictionaryDTO>> kv : all.stream().collect(Collectors.groupingBy(DictionaryDTO::getPId)).entrySet()) {
            DictionaryDTO item = new DictionaryDTO();
            item.setValue(kv.getKey());
            item.setLabel(kv.getValue().get(0).getPName());
            item.setChildren(kv.getValue());
            tree.add(item);
        }
        return tree;
    }

    @Override
    public List<DictionaryDTO> districtSelection(long code) {
        if(code != 0 && String.valueOf(code).length() == 6) {
            code = mapper.findIdByDistrictId(code);
        }
        return mapper.districtSelection(code);
    }


    /**
     * 分页查询
     * @param param 查询条件
     * @return 分页列表
     */
    @Override
    public IPage<FdJobSubcategoryVO> pageList(FdJobSubcategorySearch param) {
        //查询数据
        IPage<FdJobSubcategory> page = page(getPage(param), getWrapper(param));
        IPage<FdJobSubcategoryVO> voPage = new Page<>();
        //数据组装
        voPage.setRecords(page.getRecords().stream().map(r->{
            FdJobSubcategoryVO vo = new FdJobSubcategoryVO();
            BeanUtils.copyProperties(r,vo);
            //数据组装
            setViewParam(r,vo);
            return vo;
        }).collect(Collectors.toList()));
        voPage.setPages(page.getPages());
        voPage.setTotal(page.getTotal());
        voPage.setSize(page.getSize());
        voPage.setCurrent(page.getCurrent());
        return voPage;
    }

    /**
     * 动态条件构建
     * @param param 查询参数
     * @return 条件语句
     */
    private LambdaQueryWrapper<FdJobSubcategory> getWrapper(FdJobSubcategorySearch param){
        LambdaQueryWrapper<FdJobSubcategory> wrapper = Wrappers.lambdaQuery();
        // 主键ID
        wrapper.eq(param.getId() != null, FdJobSubcategory::getId, param.getId());
        // 职位大类ID
        wrapper.eq(param.getCategoryId() != null, FdJobSubcategory::getCategoryId, param.getCategoryId());
        // 职位小类名称
        wrapper.eq(param.getName() != null && !param.getName().isEmpty(), FdJobSubcategory::getName, param.getName());
        // 是否热门:0-非热门,1-热门
        wrapper.eq(param.getHot() != null, FdJobSubcategory::getHot, param.getHot());
        // 排序
        wrapper.eq(param.getSort() != null, FdJobSubcategory::getSort, param.getSort());
        // 描述
        wrapper.eq(param.getDescription() != null && !param.getDescription().isEmpty(), FdJobSubcategory::getDescription, param.getDescription());
        // 创建时间
        wrapper.eq(param.getCreatedTime() != null, FdJobSubcategory::getCreatedTime, param.getCreatedTime());
        // 更新时间
        wrapper.eq(param.getUpdatedTime() != null, FdJobSubcategory::getUpdatedTime, param.getUpdatedTime());
        //
        wrapper.eq(param.getDelFlag() != null, FdJobSubcategory::getDelFlag, param.getDelFlag());
        // 主键倒序
        wrapper.orderByDesc(FdJobSubcategory::getId);
        return wrapper;
    }

    /**
     * 详情
     * @param id 查询条件
     * @return 详情
    */
    @Override
    public FdJobSubcategoryVO info(Long id) {
        // 查询主键详情
        FdJobSubcategory entity = getById(id);
        if (ObjectUtils.isEmpty(entity)){
            throw new ServiceException("查询不到数据");
        }
        // 回显参数封装
        FdJobSubcategoryVO result = new FdJobSubcategoryVO();
        BeanUtils.copyProperties(entity,result);
        // 数据组装
        setViewParam(entity,result);
        return result;
    }

    /**
     * 新增
     * @param body 参数
     * @return 成功与否
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveOrUpdate(FdJobSubcategoryDTO body) {
        if (body.getId() == null){
            // 主键为空
            FdJobSubcategory entity = new FdJobSubcategory();
            BeanUtils.copyProperties(body,entity);
            // 新增
            return save(entity);
        }else {
            // 主键不为空
            FdJobSubcategory entity = getById(body.getId());
            BeanUtils.copyProperties(body,entity);
            // 更新
            return updateById(entity);
        }
    }

    /**
     * 批量删除
     * @param idList 主键列表
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(List<Long> idList) {
        //逻辑删除
        //FdJobSubcategory entity = new FdJobSubcategory();
        //entity.setDelFlag(1);
        //return update(entity,new LambdaQueryWrapper<FdJobSubcategory>().in(FdJobSubcategory::getId,idList));

        //物理删除
        return removeByIds(idList);
    }



    /**
     * 获取分页对象
     *
     * @param param 分页参数
     */
    protected IPage<FdJobSubcategory> getPage(FdJobSubcategorySearch param) {
        return new Page<>(param.getPageNum(), param.getPageSize());
    }

    /**
     * 视图数据赋值
     * @param r 数据
     * @param vo 视图
     */
    private void setViewParam(FdJobSubcategory r, FdJobSubcategoryVO vo) {
        //数据赋值业务..
    }

}
